// 引入状态管理库插件函数
import { createRouter, createWebHistory } from "vue-router";

// 引入进度条插件
import Nprogress from "nprogress";
// 引入进度条的样式
import "nprogress/nprogress.css";
// 进度条的加载小圆球不要
Nprogress.configure({ showSpinner: false });

// 引入大仓库
import pinia from "@/store/index.ts";
// 引入用户相关的仓库
import useUserStore from "@/store/modules/user.ts";
// 使用函数
const userStore = useUserStore(pinia);

// 状态管理库创建路由方法：createRouter()方法用于创建路由器实例，可以管理多个路由
const router = createRouter({
  // 路由的模式的设置
  history: createWebHistory(),
  // 管理路由
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      component: () => import("@/views/Home/index.vue"),
      meta: {
        title: "首页",
      },
    },
    {
      path: "/wxlogin",
      component: () => import("@/components/Wechat/index.vue"),
      meta: {
        title: "登录",
      },
    },
    {
      path: "/hospital",
      component: () => import("@/views/Hospital/index.vue"),
      children: [
        {
          path: "register",
          component: () => import("@/views/Hospital/ARegister/index.vue"),
          meta: {
            title: "预约挂号",
          },
        },
        {
          path: "register_step1",
          component: () =>
            import("@/views/Hospital/ARegister/register_step01.vue"),
          meta: {
            title: "预约第一步",
          },
        },
        {
          path: "register_step2",
          component: () =>
            import("@/views/Hospital/ARegister/register_step02.vue"),
          meta: {
            title: "预约第二步",
          },
        },
        {
          path: "detail",
          component: () => import("@/views/Hospital/BDetail/index.vue"),
          meta: {
            title: "医院详情",
          },
        },
        {
          path: "notice",
          component: () => import("@/views/Hospital/CNotice/index.vue"),
          meta: {
            title: "预约通知",
          },
        },
        {
          path: "close",
          component: () => import("@/views/Hospital/DClose/index.vue"),
          meta: {
            title: "停诊信息",
          },
        },
        {
          path: "search",
          component: () => import("@/views/Hospital/ESearch/index.vue"),
          meta: {
            title: "查询消息",
          },
        },
      ],
    },
    {
      path: "/user",
      component: () => import("@/views/Member/index.vue"),
      children: [
        {
          path: "certification",
          component: () => import("@/views/Member/ACertification/index.vue"),
          meta: {
            title: "实名认证",
          },
        },
        {
          path: "order",
          component: () => import("@/views/Member/BOrder/index.vue"),
          meta: {
            title: "挂号订单",
          },
        },
        {
          path: "orderDetail",
          component: () => import("@/views/Member/BOrderDetail/index.vue"),
          meta: {
            title: "订单详情",
          },
        },
        {
          path: "patient",
          component: () => import("@/views/Member/CPatient/index.vue"),
          meta: {
            title: "就诊人管理",
          },
        },
        {
          path: "profile",
          component: () => import("@/views/Member/DProfile/index.vue"),
          meta: {
            title: "账号信息",
          },
        },
        {
          path: "feedback",
          component: () => import("@/views/Member/EFeedback/index.vue"),
          meta: {
            title: "信息反馈",
          },
        },
      ],
    },
  ],
  // 滚动行为:控制滚动条的位置
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    };
  },
});

// 路由鉴权：就是路由能不能被访问到权限设置（全局守卫完成）
// 存储用户未登录可以访问路由得路径
let whiteList = [
  "/home",
  "/hospital/register",
  "/hospital/detail",
  "/hospital/notice",
  "/hospital/close",
  "/hospital/search",
];

// 添加相应的全局守卫
// 前置守卫
router.beforeEach((to, from, next) => {
  // 访问路由组件的之前,进度条开始动
  Nprogress.start();
  // 动态设置网页左上角的标题
  document.title = `招商国科-${to.meta.title}`;
  // 判断用户是否登录-token
  let token = userStore.userInfo.token;
  // 用户登陆了
  if (token) {
    next();
  } else {
    // 用户未登录
    if (whiteList.includes(to.path)) {
      next();
    } else {
      // 登录组件显示不来
      userStore.visiable = true;
      next({ path: "/home", query: { redirect: to.fullPath } });
    }
  }
});

// 后置路由
router.afterEach((to, from) => {
  // 访问路由组件成功,进度条消息
  Nprogress.done();
});

export default router;
